/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.log.server.jobhandler;

import cn.hutool.core.date.DatePattern;
import com.iwindplus.boot.util.DatesUtil;
import com.iwindplus.log.server.service.MailCaptchaLogService;
import com.iwindplus.log.server.service.SmsCaptchaLogService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * 验证码日志job.
 *
 * @author zengdegui
 * @since 2024/11/19 01:42
 */
@Component
public class CaptchaLogJob {

    @Resource
    private SmsCaptchaLogService smsCaptchaLogService;

    @Resource
    private MailCaptchaLogService mailCaptchaLogService;

    /**
     * 清理过期的数据.
     *
     * @param param 参数
     * @throws Exception
     */
    @XxlJob("removeTimeoutDataJobHandler")
    public void removeTimeoutDataJobHandler(String param) {
        long beginMillis = System.currentTimeMillis();

        XxlJobHelper.log("开始时间={}", DatesUtil.parseDate(beginMillis, DatePattern.NORM_DATETIME_MS_PATTERN));
        final boolean smsCaptchaLogResult = this.smsCaptchaLogService.removeTimeoutData();
        XxlJobHelper.log("短信日志清理执行结果={}", smsCaptchaLogResult);

        final boolean mailCaptchaLogResult = this.mailCaptchaLogService.removeTimeoutData();
        XxlJobHelper.log("邮箱日志清理执行结果={}", mailCaptchaLogResult);

        // 结束日志
        final long endTimeMillis = System.currentTimeMillis();
        XxlJobHelper.log("结束时间={}，总执行毫秒数={}", DatesUtil.parseDate(endTimeMillis, DatePattern.NORM_DATETIME_MS_PATTERN), endTimeMillis - beginMillis);
    }
}
